package com.eduagent.xwqeduagent.controller;

import com.eduagent.xwqeduagent.app.student.UniversalLearningAssistant.UniversalLearningAssistant;
import com.eduagent.xwqeduagent.app.student.ExerciseEvaluationAssistant.ExerciseEvaluationAssistant;
import com.eduagent.xwqeduagent.common.BaseResponse;
import com.eduagent.xwqeduagent.common.ResultUtils;
import com.eduagent.xwqeduagent.model.dto.ImmersiveVideoLearningRequest;
import com.eduagent.xwqeduagent.model.dto.ExerciseEvaluationRequest;
import com.eduagent.xwqeduagent.model.vo.ImmersiveVideoLearningVO;
import com.eduagent.xwqeduagent.model.vo.ExerciseEvaluationVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;


@RestController
@RequestMapping("/student-learning")
@Slf4j
public class StudentController {

    @Resource
    private UniversalLearningAssistant universalLearningAssistant;
    
    @Resource
    private ExerciseEvaluationAssistant exerciseEvaluationAssistant;

    /**
     * 边看边问 - 视频学习中的提问
     *
     * @param request 提问请求
     * @return 回答结果
     */
    @PostMapping("/ask")
    public BaseResponse<ImmersiveVideoLearningVO> askQuestion(@RequestBody ImmersiveVideoLearningRequest request) {
        log.info("收到沉浸式学习提问请求: {}", request.getQuestion());

        // 调用UniversalLearningAssistant进行问题解答
        ImmersiveVideoLearningVO response = universalLearningAssistant.askWhileWatching(
                request.getQuestion(),
                request.getVideoTranscript(),
                request.getSessionId()
        );

        return ResultUtils.success(response);
    }
    
    /**
     * 实时练习评测 - 处理各类练习相关请求
     *
     * @param request 练习请求
     * @return 评测结果
     */
    @PostMapping("/exercise-evaluation")
    public BaseResponse<ExerciseEvaluationVO> processExerciseEvaluation(@RequestBody ExerciseEvaluationRequest request) {
        log.info("收到实时练习评测请求，类型: {}", request.getRequestType());

        ExerciseEvaluationVO response;
        
        switch (request.getRequestType()) {
            case "QUESTION":
                // 处理学生提问，提供解题思路
                response = exerciseEvaluationAssistant.provideExerciseGuidance(
                        request.getContent(),
                        request.getExerciseContent(),
                        request.getSubject(),
                        request.getSessionId()
                );
                break;
                
            case "EVALUATE":
                // 判断是单题评估还是多题评估
                if (!CollectionUtils.isEmpty(request.getSubmissions())) {
                    // 多题评估
                    response = exerciseEvaluationAssistant.evaluateMultipleAnswers(
                            request.getSubmissions(),
                            request.getSubject(),
                            request.getSessionId()
                    );
                } else {
                    // 单题评估
                    response = exerciseEvaluationAssistant.evaluateStudentAnswer(
                            request.getContent(),
                            request.getExerciseContent(),
                            request.getCorrectAnswer(),
                            request.getSubject(),
                            request.getSessionId()
                    );
                }
                break;
                
            case "SUMMARY":
                // 总结学习情况
                if (request.getSessionResult() != null) {
                    // 使用前端传入的会话结果生成总结
                    response = exerciseEvaluationAssistant.summarizeLearningSessionWithResults(
                            request.getSessionResult(),
                            request.getSubject(),
                            request.getSessionId()
                    );
                } else {
                    // 使用服务端存储的会话历史生成总结
                    response = exerciseEvaluationAssistant.summarizeLearningSession(
                            request.getSessionId()
                    );
                }
                break;
                
            case "REINFORCE":
                // 生成强化训练题目
                if (request.getSessionResult() != null) {
                    // 使用前端传入的会话结果生成强化训练
                    response = exerciseEvaluationAssistant.generateReinforcementExercisesWithResults(
                            request.getSessionResult(),
                            request.getSubject(),
                            request.getSessionId()
                    );
                } else {
                    // 使用服务端存储的会话历史生成强化训练
                    response = exerciseEvaluationAssistant.generateReinforcementExercises(
                            request.getSubject(),
                            request.getSessionId()
                    );
                }
                break;
                
            default:
                log.error("未知的请求类型: {}", request.getRequestType());
                throw new IllegalArgumentException("不支持的请求类型: " + request.getRequestType());
        }
        
        return ResultUtils.success(response);
    }
} 